package flc.ast.adapter;

import android.view.View;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import flc.ast.R;
import flc.ast.bean.MyAudioBean;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;

public class AudioAdapter extends StkProviderMultiAdapter<MyAudioBean> {

    public AudioAdapter() {
        addItemProvider(new StkSingleSpanProvider<>(40));
        addItemProvider(new AudioProvider());
    }

    private boolean isMultiple = false;

    public void setMultiple(boolean multiple) {
        isMultiple = multiple;
    }

    private class AudioProvider extends BaseItemProvider<MyAudioBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_audio;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, MyAudioBean bean) {
            baseViewHolder.setText(R.id.tvAudioItemName, bean.getName());
            baseViewHolder.setImageResource(R.id.ivAudioItemImg, bean.getIcon());
            baseViewHolder.setText(R.id.tvAudioItemDuration, bean.getDuration());
            if (bean.getTag().length == 2) {
                baseViewHolder.setImageResource(R.id.ivAudioItemTag1, bean.getTag()[0]);
                baseViewHolder.setImageResource(R.id.ivAudioItemTag2, bean.getTag()[1]);
                baseViewHolder.getView(R.id.ivAudioItemTag2).setVisibility(View.VISIBLE);

            } else {
                baseViewHolder.setImageResource(R.id.ivAudioItemTag1, bean.getTag()[0]);
                baseViewHolder.getView(R.id.ivAudioItemTag2).setVisibility(View.GONE);
            }
            if (bean.isPlaying()) {
                baseViewHolder.getView(R.id.ivAudioItemPlay).setVisibility(View.VISIBLE);
            } else {
                baseViewHolder.getView(R.id.ivAudioItemPlay).setVisibility(View.GONE);
            }
            if (isMultiple) {
                baseViewHolder.getView(R.id.ivAudioItemSel).setVisibility(View.VISIBLE);
                if (bean.isSelected()) {
                    baseViewHolder.setImageResource(R.id.ivAudioItemSel, R.drawable.audio_sel);
                } else {
                    baseViewHolder.setImageResource(R.id.ivAudioItemSel, R.drawable.audio_not_sel);
                }
            }else {
                baseViewHolder.getView(R.id.ivAudioItemSel).setVisibility(View.GONE);
            }
        }
    }

}
